import { fileURLToPath, URL } from 'node:url'
import { defineConfig, loadEnv } from 'vite'
import uni from '@dcloudio/vite-plugin-uni'
// vue和uniapp自动导入插件
import AutoImport from 'unplugin-auto-import/vite'
// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    uni(),
    // 自动导入
    AutoImport({
      imports: ['vue', 'uni-app'],
    }),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
    },
  },
  // 跨域代理请求
  server: {
    proxy: {
      '/api': {
        // target: import.meta.env.VITE_API_URL,
        target: loadEnv('', process.cwd()).VITE_API_URL, // 获取环境变量 读取env文件 VITE_API_URL的值
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ''),
      },
    },
  },
})
